package nl.scalasim
package actors
package citizenactivities

import nl.scalasim.util.Accumulator

/**
 * Activity for an actor that works at a harvester.
 *
 * @author Yigal Duppen
 * @since Oct 28, 2010
 */
class AtWorkAtHarvesterActivity(val citizen: Citizen) extends Activity {

  private var accumulator: Accumulator = _

  def activate = {
    accumulator = Accumulator(citizen.ticksAtWork)
  }

  def handleAct = {
    accumulator = accumulator.accumulate
    accumulator.overflow match {
      case 1 => {
        goHome
      }
      case _ => {
        harvest
      }
    }
  }

  private def goHome = {
    log.fine("Work time is over, moving to " + citizen.house)
    citizen.currentActivity = citizen.commuting
    citizen.commuting.nextActivity = citizen.atHome
    citizen.addressBook.choreographer ! MoveTo(citizen, citizen.house)
    done()
  }

  private def harvest = {
    log.info(citizen + " is harvesting at " + citizen.workplace)
    citizen.workplace.get ! Process
    react {
      case Stored(_, _) => {
        done()
      }
    }
  }

}